package cn.itcast.cart.service.impl;

import cn.itcast.cart.dto.ShoppingCartDTO;
import cn.itcast.cart.mapper.ShoppingCartMapper;
import cn.itcast.cart.pojo.ShoppingCart;
import cn.itcast.cart.service.ShoppingCartService;
import cn.itcast.context.BaseContext;
import cn.itcast.goods.feign.GoodsClient;
import cn.itcast.goods.pojo.Dish;
import cn.itcast.goods.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private GoodsClient goodsClient;



    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = new ShoppingCart();

        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());


        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        if (list != null && list.size() > 0) {
            shoppingCart = list.get(0);
            shoppingCartMapper.update(ShoppingCart.builder()
                    .id(shoppingCart.getId())
                    .number(shoppingCart.getNumber() + 1)
                    .build());
            return;
        }


        if (shoppingCart.getDishId() != null) {

            Dish dish = goodsClient.getDishById(shoppingCart.getDishId());
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());
        }
        if (shoppingCart.getSetmealId() != null) {
            SetmealVO setmealVO = goodsClient.getSetmealVOById(shoppingCart.getSetmealId());
            shoppingCart.setName(setmealVO.getName());
            shoppingCart.setImage(setmealVO.getImage());
            shoppingCart.setAmount(setmealVO.getPrice());
        }


        shoppingCart.setNumber(1);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setCreateTime(LocalDateTime.now());

        shoppingCartMapper.insert(shoppingCart);

    }

    /**
     * 查询购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {


        return shoppingCartMapper.list(ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build());
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        shoppingCartMapper.delete(ShoppingCart.builder().userId(BaseContext.getCurrentId()).build());
    }

    /**
     * 删除购物车中单个的商品
     *
     * @param shoppingCartDTO
     */
    @Override
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list != null && (shoppingCart = list.get(0)).getNumber() > 1) {
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartMapper.update(shoppingCart);
            return;
        }

        shoppingCartMapper.delete(shoppingCart);

    }

    /**
     * 查询购物车
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public List<ShoppingCart> getList(ShoppingCart shoppingCart) {
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 清空购物车
     *
     * @param shoppingCart
     */
    @Override
    public void delete(ShoppingCart shoppingCart) {
        shoppingCartMapper.delete(shoppingCart);
    }
}